package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.FlexibleRoundeBitmapDisplayer;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.bean.GiftObject;
import com.lishe.lishe.lishe.utils.Utils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by shino on 2017/10/24.
 */

public class SelectGiftAdapter extends RecyclerviewBaseAdapter {
    private Context context;
    private LinkedList<GiftObject> images;
    private ImageLoaderUtils imageLoaderUtils;
    private int lastSel = -1;
    private int mDisplayWidth;

    public SelectGiftAdapter(Context context, LinkedList<GiftObject> images, int displayWidth) {
        this.context = context;
        this.images = images;
        this.mDisplayWidth = displayWidth;
        imageLoaderUtils = new ImageLoaderUtils(Utils.dip2px(context, 5), FlexibleRoundeBitmapDisplayer.CORNER_TOP_LEFT_RIGHT);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_person_gift, null);
        SelectGiftHolder holder = new SelectGiftHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {//当view没有绑定到screen时，payloads就会失效被丢弃
            onBindViewHolder(holder, position);
        } else {
            try {
                updateContent(holder, position);
            } catch (Exception e) {
                e.printStackTrace();
                onBindViewHolder(holder, position);
            }
        }
    }

    private void updateContent(RecyclerView.ViewHolder holder, int position) throws Exception {
        GiftObject giftObject = images.get(position);
        if (giftObject.isSelect()) {
            ((SelectGiftHolder) holder).selectImage.setImageResource(R.mipmap.sel_choice_icon);
        } else {
            ((SelectGiftHolder) holder).selectImage.setImageBitmap(null);
        }
    }

    @Override
    public void onBindViewHolderMy(final RecyclerView.ViewHolder holder, final int position) {
        final SelectGiftHolder holder0 = (SelectGiftHolder) holder;
        final GiftObject giftObject = images.get(position);
        imageLoaderUtils.displayImage(giftObject.getGiftImgUrl(), holder0.image);
        holder0.name.setText(giftObject.getGiftName());
        holder0.integral.setText((int) giftObject.getGiftCost() + "积分");
        if (giftObject.isSelect()) {
            holder0.selectImage.setImageResource(R.mipmap.sel_choice_icon);
        } else {
            holder0.selectImage.setImageBitmap(null);
        }
        holder0.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setOnItemClickListener(SelectGiftAdapter.this, holder, position);
                if (lastSel != -1) {
                    images.get(lastSel).setSelect(false);
                }
                lastSel = position;
                giftObject.setSelect(!giftObject.isSelect());
                notifyItemRangeChanged(0, images.size(), "lishe");
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return images == null ? 0 : images.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }


    class SelectGiftHolder extends RecyclerView.ViewHolder {
        private ImageView image;
        private ImageView selectImage;
        private TextView name;
        private TextView integral;
        private TextView num;
//        private RelativeLayout relative;

        public SelectGiftHolder(View v) {
            super(v);
            image = v.findViewById(R.id.img);
            selectImage = v.findViewById(R.id.check);
            name = v.findViewById(R.id.name);
            integral = v.findViewById(R.id.gift_cost);
            num = v.findViewById(R.id.num);
            num.setVisibility(View.GONE);
//            relative = v.findViewById(R.id.relative);

            int width = (mDisplayWidth - Utils.dip2px(context, 10) * 4) / 3;
            image.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
//            relative.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
        }
    }

    @Override
    public void setOnItemListener(RecyclerView.ViewHolder holder, int position) {
        super.setOnItemListener(holder, position);
    }
}
